package src

import (
	"fmt"
	"mon-go/src/utils"
	"os"
	"path"
	"strings"
	"time"
)

type Config struct {
	Company     string
	Module      string
	Version     string
	Domain      string
	Env         string
	ExePath     string
	HomeDir     string
	ReplicasNum int
}

func (config *Config) getJarFileName() string {
	return fmt.Sprintf("%s-%s.jar", "guofu", config.Module)
}

func (config *Config) getEnvJarFileName() string {
	return fmt.Sprintf("%s-%s-%s-%s.jar", "guofu", config.Module, config.Env, config.Version)
}

func (config *Config) GetJarFilePath() string {
	FileName := config.getJarFileName()
	return path.Join(config.HomeDir, config.Env, config.Module, FileName)
}

func (config *Config) IsProd() bool {
	return config.Env == "prod"
}

func (config *Config) IsPre() bool {
	return config.Env == "pre"
}

func (config *Config) GetCpuNum() int {
	if config.IsProd() {
		return 2
	}
	return 1
}

func (config *Config) GetReplicasNum() int {
	if config.IsProd() {
		if config.ReplicasNum > 1 {
			return config.ReplicasNum
		}
		return 2
	}
	return 1
}

func (config *Config) ReCalc() {
	config.CalcCompany()
	config.ReplicasNum = config.GetReplicasNum()
}

func (config *Config) GetConfigName() string {
	return fmt.Sprintf("%s-%s", config.Company, "config")
}

func (config *Config) CalcCompany() {
	companys := []string{"guofu", "zhengshang"}
	if utils.NotContains(companys, config.Company) {
		return
	}
	modules := []string{"search", "goods"}
	if utils.Contains(modules, config.Module) {
		config.Company = "guofu"
		return
	}
	config.Company = "zhengshang"
}

func (config *Config) GetContextPath() string {
	return path.Join(config.HomeDir, config.Env, config.Module)
}

func (config *Config) WaitForSeconds(seconds int) {
	spinner := []string{"-", "\\", "|", "/"}
	for i := 0; i < seconds*4; i++ {
		fmt.Printf("\rWaiting...... %s", spinner[i%len(spinner)])
		time.Sleep(250 * time.Millisecond)
	}
	fmt.Printf("\rSleep Done          \n")
}

func (config *Config) Rename() {
	contextPath := config.GetContextPath()
	oldPath := path.Join(contextPath, config.getJarFileName())
	newPath := path.Join(contextPath, config.getEnvJarFileName())
	err := os.Rename(oldPath, newPath)
	if err != nil {
		fmt.Println("Error renaming file:", err)
	}
}

func (config *Config) Delete() {
	contextPath := config.GetContextPath()
	oldPath := path.Join(contextPath, config.getJarFileName())
	newPath := path.Join(contextPath, config.getEnvJarFileName())
	err := os.Rename(oldPath, newPath)
	if err != nil {
		fmt.Println("Error renaming file:", err)
	}
}

func (config *Config) FindAllExtFiles(dir string, ext string) []string {
	var jarFiles []string
	entries, err := os.ReadDir(dir)
	if err != nil {
		fmt.Println("读取目录时出错:", err)
		return jarFiles
	}
	for _, entry := range entries {
		if !entry.IsDir() {
			name := entry.Name()
			if strings.HasSuffix(strings.ToLower(name), ext) {
				jarFiles = append(jarFiles, path.Join(dir, name))
			}
		}
	}
	return jarFiles
}

func (config *Config) DeleteNDayBefore(days int, jarFiles []string) {
	cutoff := time.Now().AddDate(0, 0, -days)
	for _, fullPath := range jarFiles {
		fileInfo, err := os.Stat(fullPath)
		if err != nil {
			fmt.Printf("获取文件 %s 信息时出错: %v\n", fullPath, err)
			continue
		}
		if fileInfo.ModTime().Before(cutoff) {
			err := os.Remove(fullPath)
			if err != nil {
				fmt.Printf("删除文件 %s 时出错: %v\n", fullPath, err)
			} else {
				fmt.Printf("已删除文件: %s\n", fullPath)
			}
		}
	}
}

func (config *Config) DiffFileModTime(filePath string) float64 {
	fileInfo, err := os.Stat(filePath)
	if err != nil {
		return 0
	}
	modTime := fileInfo.ModTime()
	now := time.Now()
	sub := now.Sub(modTime)
	return sub.Minutes()
}

func (config *Config) CheckJarExist() bool {
	filePath := config.GetJarFilePath()
	_, err := os.Stat(filePath)
	if err == nil {
		return true
	}
	return false
}

func (config *Config) CalcK8sModuleName() string {
	if config.Module == "supply" {
		return "gf" + "-" + config.Module
	} else {
		return config.Company + "-" + config.Module
	}
}
